#include "QHutu.h"

#include "Debugging.h"
#include "QDebugging.h"

#include <QDir>
#include <QApplication>
#include <QTextStream>

namespace Softwarer {

	QList<QString> QHutu::splitPath(const QString& path)
	{
		return path.split("/");
	}

	QString QHutu::resourceDirPath(const QString& path)
	{
		return applicationDirPath(QString("Resources/%1").arg(path));
	}

	QString QHutu::applicationDirPath(const QString& path)
	{
		QList<QString> paths = splitPath(path);
		paths.prepend(QApplication::applicationDirPath());
		return combine(paths);
	}
	QString QHutu::readTextFile(const QString& filepath)
	{
		QFile file(filepath);
		if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QTextStream in(&file);
			return in.readAll();
		}
		return QString();
	}
	QString QHutu::readResourceFile(const QString& filepath) 
	{
		return readTextFile(resourceDirPath(filepath));
	};
	Logger* m_logger = nullptr;

	Logger* QHutu::createQLogger()
	{
		if (m_logger == nullptr) 
		{
			m_logger = createLogger();
			Debugging::createDebugging()->setDisplay(new QDebugDisplay());
		}
		return m_logger;
	}
	QString QHutu::combine(const QList<QString>& paths)
	{
		QString result;
		for (QString path : paths)
		{
			result = QString::fromLocal8Bit("%1%2%3").arg(result).arg(QDir::separator()).arg(path);
		}
		return result.mid(1);

	}
	QString QHutu::combine(const initializer_list<QString>& paths)
	{
		QList<QString> results;
		for (QString path : paths)
		{
			results.append(path);
		}
		return combine(results);
	}
}